package com.woniu.permissions.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.model.Result;
import com.woniu.permissions.dto.AccountDto;
import com.woniu.permissions.form.AccountLoginForm;
import com.woniu.permissions.form.AddAccountForm;
import com.woniu.permissions.form.AddUserAccountForm;
import com.woniu.permissions.form.UpdateAccountStateForm;
import com.woniu.permissions.model.Account;
import com.woniu.permissions.param.AccountLoginParam;
import com.woniu.permissions.param.AddAccountParam;
import com.woniu.permissions.param.AddUserAccountParam;
import com.woniu.permissions.param.UpdateAccountStateParam;
import com.woniu.permissions.service.AccountService;
import com.woniu.util.RedisUtil;
import com.woniu.utils.JwtUtil;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/account")
@Api(tags = "员工账号接口")
public class AccountController {

    @Resource
    private AccountService accountService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 新增员工账号
     * @param form 密码  手机号为账号
     * @return 返回成功的对象
     */
    @ApiOperation("新增员工账号")
    @PostMapping("/addAccount")
    @GlobalTransactional
    public Result<Account> addAccount(@RequestBody AddAccountForm form,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseStationBossToken(refreshToken);
        System.out.println("用户模块test=================>"+ RootContext.getXID());
        Account account = accountService.addAccount(BeanUtil.toBean(form, AddAccountParam.class));
        return Result.newInstance(Account.class).setData(account).setMessage("新增成功");
    }

    /**
     * 根据手机账号查询员工账号
     * @param phone 手机号
     * @return 返回账号
     */
    @ApiOperation("根据手机账号查询员工账号")
    @GetMapping("/selectAccount/{phone}")
    public Result<Account> selectAccount(@PathVariable("phone") String phone,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseStationBossToken(refreshToken);
        System.out.println(phone);
        Account account =  accountService.selectAccount(phone);
        System.out.println("查询员工");
        return Result.newInstance(Account.class).setData(account).setMessage("查询成功");
    }

//    /**
//     * 测试
//     * @return 测试成功
//     */
//    @ApiOperation("测试")
//    @GetMapping("/test")
//    public Result test(){
//        String i = accountService.gettest();
//        return Result.newInstance(String.class).setData(i).setMessage("测试成功");
//    }

    /**
     * 根据账号id查询账号
     * @param accountId 账号id
     * @return 存在返回true 不存在返回false
     */
    @ApiOperation("根据账号id查询账号")
    @GetMapping("/findAccountByAccountId/{accountId}")
    public Result<Account> findAccountByAccountId(@PathVariable("accountId") String accountId){
        Account b = accountService.findAccountByAccountId(accountId);
        return Result.newInstance(Account.class).setData(b).setMessage("查询成功");
    }

    /**
     * 根据账号id修改账号状态
     * @param form 账号id 状态码
     * @return 返回修改后账号
     */
    @ApiOperation("根据账号id修改账号状态")
    @PostMapping("/updateAccountById")
    public Result<Account> updateAccountById(@RequestBody UpdateAccountStateForm form,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseStationBossToken(refreshToken);
        System.out.println("拿到的form :"+form);
        Account account = accountService.updateAccountById(BeanUtil.toBean(form, UpdateAccountStateParam.class));
        return Result.newInstance(Account.class).setData(account).setMessage("修改成功");
    }


    /**
     * 登录
     * @param form 登录对象
     * @return
     */
    @PostMapping("/login")
    public Result<AccountDto> login(@RequestBody AccountLoginForm form){
        System.out.println("拿到的form :" +form);
        AccountDto dto = accountService.login(BeanUtil.toBean(form, AccountLoginParam.class));
        return Result.newInstance(AccountDto.class).setData(dto).setMessage("查询成功");
    }

    /**
     * 根据手机号 或者 用户名查询 账号列表
     * @param phone
     * @return
     */
    @GetMapping("/byPhoneOrUsername/{phone}/{accountName}")
    @ApiOperation("根据手机号或者用户名查询账号列表")
    public Result<List<Account>> byPhoneOrUsername(@PathVariable("phone") String phone,@PathVariable("accountName") String accountName){
        QueryWrapper<Account> wrapper = new QueryWrapper<Account>();
        wrapper.eq("account_phone", phone).or().eq("account_name", accountName);
        List<Account> list = accountService.list(wrapper);
        return Result.newListInstance(Account.class).setData(list).setMessage("根据手机号或用户名查询账号列表成功");
    }


    /**
     * 根据用户名查询 账号对象
     * @param phone
     * @return
     */
    @GetMapping("/byPhone/{phone}")
    public Result<Account> byPhone(@PathVariable("phone") String phone){
        Account account = accountService.byPhone(phone);
        return Result.newInstance(Account.class).setData(account).setMessage("根据手机号或用户名查询账号列表成功");
    }


    /**
     * 新增用户账号
     * @param form
     * @return
     */
    @ApiOperation("新增用户账号")
    @PostMapping("/addUserAccount")
    @GlobalTransactional(rollbackFor = Throwable.class)
    public Result<Account> addUserAccount(@RequestBody AddUserAccountForm form) throws Exception {

        System.out.println("用户模块test=================>"+ RootContext.getXID());
        Account account = accountService.addUserAccount(BeanUtil.toBean(form, AddUserAccountParam.class));
        return Result.newInstance(Account.class).setData(account).setMessage("新增成功");
    }

    /**
     * 修改用户密码
     * @param accountId 账号id
     * @param newPassword 新密码
     * @return
     */
    @GetMapping("/modifyUserPass/{accountId}/{newPassword}")
    public Result<?> modifyUserPass(@PathVariable("accountId") Integer accountId,@PathVariable("newPassword") String newPassword) throws Exception {
        Account account = accountService.getById(accountId);
        if (account==null) {
            throw new Exception("账号不存在");
        }
        account.setAccountPass(newPassword);
        accountService.updateById(account);
        return Result.newInstance(null).setMessage("修改密码成功");
    }

}

